﻿using System;
using System.Messaging;
using System.Threading;
using System.Windows.Forms;
using Message = System.Messaging.Message;

namespace clientMSMQ
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }
        /// <summary>
        /// 接收
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            string ekQ = $@"FormatName:Direct=TCP:{textBox5.Text.Trim()}\Private$\{textBox4.Text.Trim()}";
            MessageQueue myQueue = new MessageQueue(ekQ);
            try
            {
                // 创建消息队列对象
                using (myQueue)
                {
                    //mq.BeginReceive();
                    // 设置消息队列的格式化器

                    //Message msg1 = mq.Receive();
                    // msg1.Formatter = new XmlMessageFormatter(new Type[] { typeof(String) });
                    // string s = msg1.Body.ToString();

                    myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });//mq.Formatter = new XmlMessageFormatter(new string[] { "System.String" });
                    foreach (Message msg in myQueue.GetAllMessages())
                    {
                        listBox1.Items.Add(msg.Body);
                        myQueue.Receive();//接收完成后，就销毁了
                    }

                    // 获得消息队列中第一条可用消息
                    //Message firstmsg = mq.Receive();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex + ex.Message);
            }

        }

        /// <summary>
        /// 发送
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string ekQ = $@"FormatName:Direct=TCP:{textBox5.Text.Trim()}\Private$\{textBox4.Text.Trim()}";
                MessageQueue myQueue = new MessageQueue();
                myQueue.Path = ekQ;
                Message ms = new Message();
                MessageQueueTransaction mt = new MessageQueueTransaction();
                mt.Begin();

                ms.Body = $"{textBox3.Text}|{textBox1.Text}|{textBox2.Text}";
                myQueue.Send(ms, mt);

                mt.Commit();
                ms.Dispose();
                myQueue.Dispose();
                myQueue.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            ;
            myQueue1.Dispose();
            myQueue1.Close();
        }
        MessageQueue myQueue1 = new MessageQueue();
        private void button8_Click(object sender, EventArgs e)
        {
            string ekQ = $@"FormatName:Direct=TCP:{textBox5.Text.Trim()}\private$\{textBox4.Text.Trim()}";
            myQueue1.Path=ekQ;
            myQueue1.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
            //MessageQueueTransaction mt = new MessageQueueTransaction();
            //mt.Begin();


            //mt.Commit();
            myQueue1.ReceiveCompleted += MyQueue_ReceiveCompleted;
            myQueue1.BeginReceive();     //开始接收
        }

        private void MyQueue_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
        {
            if (!myQueue1.CanRead)
            {
                return;
            }
            Invoke(new Action(() => listBox1.Items.Add(e.Message.Body.ToString())));
            myQueue1.BeginReceive();       //接收
        }
    }
}
